package CreatorUtil;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import persistent.Db4oServletListener;

import com.db4o.ObjectContainer;
import com.db4o.ObjectSet;
import com.db4o.query.Predicate;

import LoginUtil.*;
import LoginUtil.User.UserType;
import persistent.Patient;

/**
 * Servlet implementation class LoginServlet
 */
public class CreateNewUserServlet extends HttpServlet {


public void doGet(HttpServletRequest request, HttpServletResponse response) 
			           throws ServletException, java.io.IOException {

try
{	    
	ObjectContainer container = 
        (ObjectContainer)request.getAttribute(Db4oServletListener.KEY_DB4O_SESSION);
	
	final String username = request.getParameter("usernamebox");
	final String password = request.getParameter("passwordbox");
	final UserType userType = parseUserType(request.getParameter("usertype"));
	
	ObjectSet<User> rs = container.query(new Predicate<User>(){
			public boolean match(User user) {
				return user.getUsername().equals(username);}});
	
	// check if the username is unique first
	// if it isn't assume updating Patient
	if(rs.size() <= 0) {
		
		
		if(!password.equals("")) {	// then check that the password is not blank
			if(userType == UserType.Patient) {
				Patient patient = new Patient(username, password);
				patient.setFirstName(request.getParameter("firstnamebox"));
			    patient.setLastName(request.getParameter("lastnamebox"));
			    patient.setUserType(userType);
			    patient.setAddress(request.getParameter("addressbox"));
			    patient.setAge(Integer.parseInt(request.getParameter("agebox")));
			    patient.setAllergies(request.getParameter("allergiesbox"));
			    patient.setEmail(request.getParameter("emailbox"));
			    patient.setGender(request.getParameter("genderbox"));
			    patient.setInsurance(request.getParameter("insurancebox"));
			    patient.setNumber(request.getParameter("phonebox"));
			    patient.setPharmacy(request.getParameter("pharmacybox"));
			    
			    container.store(patient);
			} else {
				 User user = new User(username,password);
			     user.setFirstName(request.getParameter("firstnamebox"));
			     user.setLastName(request.getParameter("lastnamebox"));
			     user.setUserType(userType);
		     
		     	container.store(user);
			}
			
	     container.commit();
	     
	     response.sendRedirect("newUserCreated.jsp");
		} else {
			
			// create an error page in response to blank password
			PrintWriter rw = response.getWriter();
			rw.append("<html><head>" +
	         "<meta http-equiv=\"Content-Type\"" + 
	            "content=\"text/html; charset=windows-1256\">" +
	         "<title>  Error - Adventure Time MedSys  </title>" +
	         "<link rel=\"stylesheet\" type=\"text/css\" href=\"style.css\" />" +
	      "</head><body><center><br><br>The password must not be blank<br>Please press back on your browser</body></center>");
			rw.close();
		}
	} else {
		
		// create an error page in response to invalid username
		PrintWriter pw = response.getWriter();
		pw.append("<html><head>" +
         "<meta http-equiv=\"Content-Type\"" + 
            "content=\"text/html; charset=windows-1256\">" +
         "<title>  Error - Adventure Time MedSys  </title>" +
         "<link rel=\"stylesheet\" type=\"text/css\" href=\"style.css\" />" +
      "</head><body><center><br><br>The username you selected is already taken<br>Please press back on your browser</body></center>");
		pw.close();
	}
} 
		
		
catch (Throwable theException) 	    
{
     System.out.println(theException); 
}
       }

private UserType parseUserType(String parameter) {
	UserType type;
	if(parameter.equalsIgnoreCase("admin")) type = UserType.Admin;
	else if(parameter.equalsIgnoreCase("doctor")) type = UserType.Doctor;
	else if(parameter.equalsIgnoreCase("nurse")) type = UserType.Nurse;
	else if(parameter.equalsIgnoreCase("patient")) type = UserType.Patient;
	else type = UserType.Invalid;
	return type;
}
	}

